package sample.viewassets.global
{
	import asf.core.Application;
	import asf.sections.Section;
	import asf.utils.FX;
	
	import br.pedromoraes.btween.ISequenceable;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import sample.app.Globals;

	public class MainMenu extends Sprite
	{
		private var app:Application;
		private var selectedItem:MainMenuItem;
		private var selectedData:Section;
		
		private var items:Array;
		
		public function MainMenu( )
		{
			super( );
			
			app = Application.getByID( Globals.APP_ID );
			
			mount( );
		}
		
		private function mount( ):void
		{
			var sections:Array = app.navigation.sections;
			var section:Section;
			var item:MainMenuItem;
			
			var n:uint = 0;
			
			items = new Array( );
			
			for each( section in sections )
			{
				item = new MainMenuItem( section );
				item.y = n * ( item.height + 5 );
				
				item.addEventListener( MouseEvent.CLICK, itemClick );
				item.alpha = 0;
				item.visible = false;
				
				items.push( item );
				addChild( item );
				
				n++;
			}
		}
		
		public function show( ):ISequenceable
		{
			var item:MainMenuItem;
			var delay:uint = 70;
			var n:uint = 0;
			
			var res:ISequenceable;
			
			for each( item in items )
			{
				res = FX.fadeIn( item, 300, delay * n );
				
				n++;
			}
			
			return res;
		}
		
		private function itemClick( evt:MouseEvent ):void
		{
			selectedItem = evt.currentTarget as MainMenuItem;
			selectedData = selectedItem.getData( );
			
			this.dispatchEvent( new Event( Event.SELECT ) );
		}
		
		public function getSelectedSection( ):Section
		{
			return selectedData;
		}
	}
}